package com.yoso.starter.boot.util;

import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Objects;


/**
 * 排序工具
 * @author shizejin
 * @date 2023/11/28 10:46
 */
@Slf4j
public class OrderUtil {

    /**
     * Java自定义排序，目前仅仅支持单个字段的排序
     *
     * @param o1      比较对象1
     * @param o2      比较对象2
     * @param orderBy 比较的排序字段，必须在T类里面包含此字段
     * @param orderRule  排序方向 默认升序
     * @return int
     * @author Michael Xu
     * @date 2019/10/29 14:20
     */
    public static <T> int orderBy(T o1, T o2, String orderBy, String orderRule) {
        Field field = Arrays.stream(o1.getClass().getDeclaredFields())
                .filter(f -> f.getName().equals(orderBy)).findFirst().orElse(null);
        if (Objects.nonNull(field)) {
            field.setAccessible(true);
            try {
                // 空值放在最后
                if (ObjectUtil.isNull(field.get(o1))) {
                    return ((ObjectUtil.isNull(field.get(o2)))) ? 0 : 1;
                } else if ((ObjectUtil.isNull(field.get(o2)))) {
                    return -1;
                }
                if (field.getType() == Integer.class) {
                    Integer valueBefore = (Integer) (field.get(o1));
                    Integer valueAfter = (Integer) (field.get(o2));
                    if ("desc".equalsIgnoreCase(orderRule)) {
                        return valueAfter.compareTo(valueBefore);
                    } else {
                        return valueBefore.compareTo(valueAfter);
                    }
                } else if (field.getType() == Long.class) {
                    Long valueBefore = (Long) (field.get(o1));
                    Long valueAfter = (Long) (field.get(o2));
                    if ("desc".equalsIgnoreCase(orderRule)) {
                        return valueAfter.compareTo(valueBefore);
                    } else {
                        return valueBefore.compareTo(valueAfter);
                    }
                } else {
                    String valueBefore = String.valueOf(field.get(o1));
                    String valueAfter = String.valueOf(field.get(o2));
                    // 字符串的先转化为BigDecimal，如果可以就用字符串的先转化为BigDecimal进行比较
                    try {
                        BigDecimal valueBeforeNumber = new BigDecimal(valueBefore);
                        BigDecimal valueAfterNumber = new BigDecimal(valueAfter);
                        if ("desc".equalsIgnoreCase(orderRule)) {
                            return valueAfterNumber.compareTo(valueBeforeNumber);
                        } else {
                            return valueBeforeNumber.compareTo(valueAfterNumber);
                        }
                    } catch (Exception e) {
                        log.debug("该字段非数字内容，用中文进行比较");
                    }
                    // 其他的转一下拼音进行比较
                    valueBefore = PinYinUtils.changeToGetShortPinYin(valueBefore);
                    valueAfter = PinYinUtils.changeToGetShortPinYin(valueAfter);
                    if ("desc".equalsIgnoreCase(orderRule)) {
                        return valueAfter.compareTo(valueBefore);
                    } else {
                        return valueBefore.compareTo(valueAfter);
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return 1;
    }
}
